package com.zhaolixiang.slidetabview

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.util.Log
import android.view.MotionEvent
import android.view.ViewTreeObserver
import com.zhaolixiang.slidetabview.son.Son1Fragment
import com.zhaolixiang.slidetabview.son.Son2Fragment
import com.zhaolixiang.slidetabview.son.Son3Fragment
import com.zhaolixiang.slidetabview.son.Son4Fragment
import kotlinx.android.synthetic.main.activity_main3.*


class Main3Activity : FragmentActivity() {
    private var count=7;
    private lateinit var activity: FragmentActivity
    private var son1Fragment:Son1Fragment?=null
    private var son2Fragment:Son2Fragment?=null
    private var son3Fragment: Son3Fragment?=null
    private var son4Fragment: Son4Fragment?=null
    private var son5Fragment:Son1Fragment?=null
    private var son6Fragment:Son2Fragment?=null
    private var son7Fragment: Son3Fragment?=null
    private var son8Fragment:Son4Fragment?=null
    private var son9Fragment:Son1Fragment?=null
    private var son10Fragment: Son2Fragment?=null
    private var son11Fragment: Son3Fragment?=null
    private var son12Fragment: Son4Fragment?=null
    private lateinit var adapter:MyPagerAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main3)
        activity=this
        for(i in 0 until count){
            AddView().addLeft(activity,ll_zong,18f)
        }
        adapter = MyPagerAdapter(this.supportFragmentManager)
        view_pager.adapter=adapter


        ll_zong.viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                ll_zong.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val width=ll_zong.getChildAt(0).width+
                        ll_zong.getChildAt(1).width+
                        ll_zong.getChildAt(2).width+
                        ll_zong.getChildAt(3).width
                for (i in 0 until ll_zong.childCount){
                    Log.e("每个控件宽短",ll_zong.getChildAt(i).width.toString())
                }
                //437
                //875
                // mHorizontalScrollView.smoothScrollBy(width,0)
                slideTabLinearLayout3.setMinWidth(
                        tv1.measuredWidth,
                        tv2.measuredWidth,
                        tv2.measuredWidth,
                        tv2.measuredWidth)

                slideTabLinearLayout3.setOnScrollEnd(object:OnScrollEnd{
                    override fun end(x: Int) {

                        //根偏移量算出对应的位置
                        val yushuo: Int = (x / tv2.measuredWidth).toInt()
                        val cha = x - tv2.measuredWidth * yushuo
                        Log.e("end求偏移量","$x---$yushuo---$cha---${tv2.measuredWidth/2}");
                        if(cha>tv2.measuredWidth/2){
                            //说明移动了一半多：移动到下一个
                            Log.e("end求偏移量1","${(yushuo+1)%4+4}")
                            view_pager.setCurrentItem((yushuo+1)%4+4,false)
                        }else{
                            //移动到上一个
                            Log.e("end求偏移量2","${(yushuo)%4+4}")
                            view_pager.setCurrentItem((yushuo)%4+4,false)
                           // view_pager.setCurrentItem(4, false);
                        }
                    }

                })
            }

        })
        view_pager.setOnTouchEventListener(object:OnTouchEventListener{
            override fun onTouchEvent(ev: MotionEvent) {
                slideTabLinearLayout3.deleteOnToucheEvent(ev)
            }

        })

        view_pager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
                Log.e("比比先后","判断当前状态$state")
                //SCROLL_STATE_DRAGGING:arg0 ==1的时表示正在滑动，SCROLL_STATE_SETTLING:arg0==2的时表示滑动完毕了，SCROLL_STATE_IDLE:arg0==0的时表示什么都没做。
                // ViewPager.SCROLL_STATE_IDLE 标识的状态是当前页面完全展现，并且没有动画正在进行中，如果不
                // 是此状态下执行 setCurrentItem 方法回在首位替换的时候会出现跳动！
               // if (state != ViewPager.SCROLL_STATE_IDLE) return;
                // 当视图在第一个时，将页面号设置为图片的最后一张。
               /* if(state==ViewPager.SCROLL_STATE_SETTLING || state==ViewPager.SCROLL_STATE_IDLE ){
                    slideTabLinearLayout3.onViewPageStop(currentPosition)
                }*/
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                // arg0 :当前页面，及你点击滑动的页面；arg1:当前页面偏移的百分比；arg2:当前页面偏移的像素位置
                Log.e("比比先后onPageScrolled多参数","position:$position---positionOffset:$positionOffset---positionOffsetPixels:$positionOffsetPixels")
               // slideTabLinearLayout3.onViewPageScroll(position,positionOffset,positionOffsetPixels,currentLast)
                /*if(Math.abs(positionOffset)>=0.5){
                    //说明需要偏转
                    view_pager.setCurrentItem(position, false);
                }*/
            }

            override fun onPageSelected(position: Int) {
                Log.e("比比先后","onPageSelected一个参数"+position)
                // arg0是当前选中的页面的Position
                if(position<4){
                    view_pager.setCurrentItem(position+4, false);
                }
                if(position>7){
                    view_pager.setCurrentItem(position-4, false);
                }
                var x=(position+1)%4
                if(x==0) x=4
                slideTabLinearLayout3.onViewPageStop(x)
            }

        })
        view_pager.setCurrentItem(4, false);
    }

    inner class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {


        override fun getCount(): Int {
            return 12
        }

        override fun getItem(position: Int): Fragment {
            Log.e("position---","position:$position")
            var position=position+1
            when(position){
                1->{
                    if (son1Fragment == null) {
                        son1Fragment = Son1Fragment()
                    }
                    return son1Fragment!!
                }
                2->{
                    if (son2Fragment == null) {
                        son2Fragment = Son2Fragment()
                    }
                    return son2Fragment!!
                }
                3->{
                    if (son3Fragment == null) {
                        son3Fragment = Son3Fragment()
                    }
                    return son3Fragment!!
                }
                4->{
                    if (son4Fragment == null) {
                        son4Fragment = Son4Fragment()
                    }
                    return son4Fragment!!
                }
                5->{
                    if (son5Fragment == null) {
                        son5Fragment = Son1Fragment()
                    }
                    return son5Fragment!!
                }
                6->{
                    if (son6Fragment == null) {
                        son6Fragment = Son2Fragment()
                    }
                    return son6Fragment!!
                }
                7->{
                    if (son7Fragment == null) {
                        son7Fragment = Son3Fragment()
                    }
                    return son7Fragment!!
                }
                8->{
                    if (son8Fragment == null) {
                        son8Fragment = Son4Fragment()
                    }
                    return son8Fragment!!
                }
                9->{
                    if (son9Fragment == null) {
                        son9Fragment = Son1Fragment()
                    }
                    return son9Fragment!!
                }
                10->{
                    if (son10Fragment == null) {
                        son10Fragment = Son2Fragment()
                    }
                    return son10Fragment!!
                }
                11->{
                    if (son11Fragment == null) {
                        son11Fragment = Son3Fragment()
                    }
                    return son11Fragment!!
                }
            }
            if (son12Fragment == null) {
                son12Fragment = Son4Fragment()
            }
            return son12Fragment!!

        }

    }
}
